<template>
  <a-card :bordered="false">
    <!-- 抽屉 -->
    <a-drawer
      :title="title"
      :width="720"
      @close="onClose"
      :visible="visible"
    >
      <!-- 抽屉内容的border -->
      <div
        :style="{
          padding:'10px',
          border: '1px solid #e9e9e9',
          background: '#fff',
        }">

        <div class="table-page-search-wrapper">
          <a-form layout="inline" @keyup.enter.native="refreshTable">
            <a-input v-model="queryParam.dictId" hidden></a-input>
            <a-row :gutter="10">
              <a-col :md="8" :sm="12">
                <a-form-item label="名称">
                  <a-input style="width: 120px;" placeholder="请输入名称" v-model="queryParam.itemText"></a-input>
                </a-form-item>
              </a-col>
              <a-col :md="9" :sm="24">
                <a-form-item label="状态" style="width: 170px" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-select
                    placeholder="请选择"
                    v-model="queryParam.status"
                  >
                    <a-select-option value="1">正常</a-select-option>
                    <a-select-option value="0">禁用</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="7" :sm="24">
                <span style="float: left;" class="table-page-search-submitButtons">
                  <a-button type="primary" @click="refreshTable">搜索</a-button>
                  <a-button type="primary" @click="searchReset" style="margin-left: 8px">重置</a-button>
                </span>
              </a-col>
            </a-row>
            <a-row>
              <a-col :md="2" :sm="24">
                <a-button style="margin-bottom: 10px" type="primary" @click="handleAdd">新增</a-button>
              </a-col>
            </a-row>
          </a-form>
        </div>
        <div>
          <s-table
            ref="table"
            :rowKey="(record) => record.id"
            :columns="columns"
            :data="loadData"
          >

            <span slot="action" slot-scope="text, record">
              <a @click="handleEdit(record)">编辑</a>
              <a-divider type="vertical"/>
              <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                <a>删除</a>
              </a-popconfirm>
            </span>

          </s-table>
        </div>
      </div>
    </a-drawer>

    <dict-item-modal ref="modalForm" @ok="modalFormOk"></dict-item-modal> <!-- 字典数据 -->

  </a-card>
</template>

<script>
import { STable } from '@/components'
import { apiDelDictItem, apiQueryDictItemList } from '@/api/system'
import DictItemModal from './modules/DictItemModal'

const columns = [{
    title: '名称',
    align: 'center',
    dataIndex: 'itemText'
  },
  {
    title: '数据值',
    align: 'center',
    dataIndex: 'itemValue'
  },
  {
    title: '操作',
    dataIndex: 'action',
    align: 'center',
    scopedSlots: { customRender: 'action' }
  }]

export default {
  name: 'DictItemList',
  components: {
    STable, DictItemModal
  },
  data () {
    return {
      /* 查询参数 */
      queryParam: {},
      labelCol: {
        xs: { span: 8 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 16 },
        sm: { span: 19 }
      },
      title: '',
      visible: false,
      columns,
      loadData: parameter => {
        return apiQueryDictItemList(Object.assign(parameter, this.queryParam)).then(res => {
          return res
        })
      }
    }
  },
  methods: {
    show (title, record) {
      this.title = title
      this.visible = true
      this.queryParam.dictId = record.id
    },
    onClose () {
      this.visible = false
    },
    refreshTable () {
      this.$refs.table.refresh(true)
    },
    searchReset () {
      const dictId = this.queryParam.dictId
      this.queryParam = { dictId }
      this.refreshTable()
    },
    handleAdd () {
      this.$refs.modalForm.show('新增', { dictId: this.queryParam.dictId, status: 1 })
    },
    handleEdit (record) {
      this.$refs.modalForm.show('编辑', record)
    },
    handleDelete (id) {
      apiDelDictItem({ id: id }).then(res => {
        console.log(res)
      }).finally(() => {
        this.refreshTable()
      })
    },
    modalFormOk () {
      this.refreshTable()
    }
  }
}
</script>

<style scoped>

</style>
